﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CRM_2013
{
    public partial class contactsCtr : DevExpress.XtraEditors.XtraUserControl
    {
        public contactsCtr()
        {
            InitializeComponent();
        }

        private void contacts_Load(object sender, EventArgs e)
        {
            contactsTA.Fill(dataSet1.Contacts);
            membersTA.FillAll(dataSet1.Members);
            paymentsTA.FillAll(dataSet1.Payments);
            sourceTA.Fill(dataSet1.Source);
        }

        public void UpdateDataContacts()
        {
            contactsTA.Update(dataSet1.Contacts);
            paymentsTA.Update(dataSet1.Payments);
        }

        private void gridViewMainContacts_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            int rowId = e.RowHandle;
            Guid id = Guid.NewGuid();
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            view.SetRowCellValue(rowId, view.Columns["idContacts"], id);
            view.SetRowCellValue(rowId, view.Columns["Mark"], 0);
            
        }

        private void gridViewPayments_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView childView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView parentView = childView.ParentView as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = e.RowHandle;

            Guid parentId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["idContacts"]);

            childView.SetRowCellValue(rowHandle, childView.Columns["idPayments"], Guid.NewGuid());
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Contacts"], parentId);
            childView.SetRowCellValue(rowHandle, childView.Columns["Date"], DateTime.Now);
        }

        private void gridViewCredits_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView childView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView parentView = childView.ParentView as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = e.RowHandle;

            Guid parentId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["idContacts"]);

            childView.SetRowCellValue(rowHandle, childView.Columns["idCredits"], Guid.NewGuid());
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Contacts"], parentId);
            childView.SetRowCellValue(rowHandle, childView.Columns["Date"], DateTime.Now);
        }

    }
}
